
package com.feib.soeasy.dao.hibernate;

import org.hibernate.Query;
import org.springframework.dao.support.DataAccessUtils;
import org.springframework.stereotype.Repository;

import com.feib.soeasy.dao.MailQueueDao;
import com.feib.soeasy.model.MailQueue;


/**
 * @title (#)MailQueueHibernateDao.java<br>
 * @description <br>
 * @author Jimmy Liu<br>
 * @version 1.0.0 2010/11/25
 * @copyright Far Eastern International Bank Copyright (c) 2010<br>
 * @2010/12/4 create by Jimmy Liu<br>
 */
@Repository("mailQueueDao")
public class MailQueueHibernateDao extends BaseHibernateDao<MailQueue, Long>  implements MailQueueDao {

    /* (non-Javadoc)
     * @see com.feib.soeasy.dao.MailQueueDao#findUnSendMail()
     */
    @SuppressWarnings("unchecked")
    @Override
    public MailQueue findUnSendMail() {
        StringBuffer hql = new StringBuffer();
        hql.append("from MailQueue ");
        hql.append("where mailStatus = 0 ");
        Query query = getSession().createQuery(hql.toString());
        query.setMaxResults(1);
        return (MailQueue)DataAccessUtils.singleResult(query.list());
    }

    /* (non-Javadoc)
     * @see com.feib.soeasy.dao.MailQueueDao#updateMailStatus(com.feib.soeasy.model.MailQueue)
     */
    @Override
    public MailQueue updateMailStatus(MailQueue mailQueue) {
        
        StringBuffer hql = new StringBuffer();
        hql.append("update MailQueue set mailStatus = :mailStatus ");
        hql.append("where mailQueuePk = :mailQueuePk ");
        Query query = getSession().createQuery(hql.toString());
        query.setParameter("mailStatus", mailQueue.getMailStatus());
        query.setParameter("mailQueuePk", mailQueue.getMailQueuePk());
        query.executeUpdate();
        
        return mailQueue;
    }


}
